package com.example.wallpaper_client_plugin;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import androidx.annotation.NonNull;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.zero.zerolivewallpaper.Utils;
import com.zero.zerolivewallpaper.data.Catalog;
import com.zero.zerolivewallpaper.data.CatalogItem;

import java.io.IOException;
import java.util.Map;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;

/** WallpaperClientPlugin */
public class WallpaperClientPlugin implements FlutterPlugin, MethodCallHandler, ActivityAware {
  /// The MethodChannel that will the communication between Flutter and native Android
  ///
  /// This local reference serves to register the plugin with the Flutter Engine and unregister it
  /// when the Flutter Engine is detached from the Activity
  private MethodChannel channel;
  private Activity flutterActivity;

  private SharedPreferences sharedPreferences;

  @Override
  public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
    channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "wallpaper_client_plugin");
    channel.setMethodCallHandler(this);
  }

  @Override
  public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
    Context context = flutterActivity.getApplicationContext();
    if (call.method.equals("getPlatformVersion")) {
      result.success("Android " + android.os.Build.VERSION.RELEASE);
    } else if(call.method.equals("sensorsAvailable")) {
      boolean flag = Utils.sensorsAvailable(context);
      result.success(flag);
    } else if(call.method.equals("downloadWallpaper")) {
      Map<String, Object> arguments = (Map<String, Object>)call.arguments;
      Gson gson = new GsonBuilder().setPrettyPrinting().create();
      CatalogItem catalogItem = gson.fromJson(gson.toJson(arguments), CatalogItem.class);
      Utils.downloadWallpaper(flutterActivity, channel, catalogItem);
    } else if(call.method.equals("setWallpaper")) {
      Map<String, Object> arguments = (Map<String, Object>)call.arguments;
      Gson gson = new GsonBuilder().setPrettyPrinting().create();
      CatalogItem catalogItem = gson.fromJson(gson.toJson(arguments), CatalogItem.class);
      try {
        Utils.setWallpaper(flutterActivity, sharedPreferences, catalogItem);
      } catch (IOException e) {
        throw new RuntimeException(e);
      }
    } else {
      result.notImplemented();
    }
  }

  @Override
  public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
    channel.setMethodCallHandler(null);
  }

  @Override
  public void onAttachedToActivity(ActivityPluginBinding binding) {
    flutterActivity = binding.getActivity();
    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(flutterActivity.getApplicationContext());
  }

  @Override
  public void onReattachedToActivityForConfigChanges(ActivityPluginBinding binding) {
    flutterActivity = binding.getActivity();
    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(flutterActivity.getApplicationContext());
  }

  @Override
  public void onDetachedFromActivity() {
    flutterActivity = null;
    sharedPreferences = null;
  }

  @Override
  public void onDetachedFromActivityForConfigChanges() {
    flutterActivity = null;
    sharedPreferences = null;
  }

}
